package com.papper.common.config;

import com.google.common.collect.Maps;
import com.papper.common.bean.sys.SysConfig;
import com.papper.common.service.sys.SysConfigService;
import com.papper.common.utils.PapperSpringUtil;
import com.papper.common.utils.tool.StringTool;

import java.util.List;
import java.util.Map;

/**
 * @Author:Lajiao
 * @Date:2014年5月20日
 * @Time:下午5:58:45
 * @Description:系统配置信息
 */
public class SysConfigHelp {
	private static       SysConfigHelp       instance           = null;
	private final static String              DEFAULT_NULL_VALUE = "NULL";
	private static       Map<String, String> Config             = Maps.newHashMap();

	private SysConfigHelp() {
	}

	public static synchronized SysConfigHelp I() {
		if (instance == null) {
			instance = new SysConfigHelp();
			instance.initPropertys();
		}
		return instance;
	}

	public void initPropertys() {
		Config.clear();
		try {
			// 系统配置
			SysConfigService s           = (SysConfigService) PapperSpringUtil.getBean("sysConfigService");
			List<SysConfig>  lstProperty = s.findAll();
			if (lstProperty != null && !lstProperty.isEmpty()) {
				Map<Long, String> topKey = Maps.newHashMap();
				for (SysConfig p : lstProperty) {
					topKey.put(p.getId(), p.getCodeKey());
				}
				String key   = null;
				String value = null;
				for (SysConfig p : lstProperty) {
					if (p.getParentId().intValue() == 0) {
						continue;
					}
					key = StringTool.nullToEmpty(topKey.get(p.getParentId().longValue())) + "." + p.getCodeKey();
					value = p.getCodeValue();
					Config.put(key, value);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public String TGS(String tk, String k, String defaultValue) {
		String rtn = Config.get(tk + "." + k);
		if (rtn != null) {
			if (DEFAULT_NULL_VALUE.equals(rtn)) {
				return defaultValue;
			}
			return rtn;
		}
		return defaultValue;
	}

	public Double TGD(String tk, String k, Double defaultValue) {
		String rtn = Config.get(tk + "." + k);
		if (rtn != null) {
			if (DEFAULT_NULL_VALUE.equals(rtn)) {
				return null;
			}
			return StringTool.parseDouble(rtn, defaultValue);
		}
		return StringTool.parseDouble(rtn, defaultValue);
	}

	public Integer TGI(String tk, String k, Integer defaultValue) {
		String rtn = Config.get(tk + "." + k);
		if (rtn != null) {
			if (DEFAULT_NULL_VALUE.equals(rtn)) {
				return null;
			}
			return StringTool.parseInteger(rtn, defaultValue);
		}
		return StringTool.parseInteger(rtn, defaultValue);
	}

	public String GS(String key) {
		String rtn = Config.get("System." + key);
		if (rtn != null) {
			if (DEFAULT_NULL_VALUE.equals(rtn)) {
				return null;
			}
			return rtn;
		}
		return rtn;
	}

	public String GS(String key, String def) {
		String rtn = Config.get("System." + key);
		if (rtn != null) {
			if (DEFAULT_NULL_VALUE.equals(rtn)) {
				return def;
			}
			return rtn;
		}
		return def;
	}

	public int GI(String key) {
		return StringTool.parseInteger(GS(key), null);
	}

	public int GI(String key, int def) {
		return StringTool.parseInteger(GS(key), def);
	}

	public double GD(String key, double def) {
		return StringTool.parseDouble(GS(key), def);
	}
}
